/**
 * @author yxw
 * date : 2013年11月4日 下午3:34:17 
 */
package com.tzinfo.tzt.activity;


import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.view.MyProgressDialog;
import com.tzinfo.tzt.config.AppDataConfig;
import com.tzinfo.tzt.domain.MyResultList;
import com.tzinfo.tzt.domain.OldhouseAgent;
import com.tzinfo.tzt.domain.OldhouseList;
import com.tzinfo.tzt.util.HttpTools;
import com.tzinfo.tzt.util.JsonTools;

public class AgentDetailActivity extends BaseActivity implements OnClickListener{
	
	
	//intent接收
	private Intent thisIntent;
	private int agentId;
	private String agentTel;
	private OldhouseList oldhouseList;
	
	//UI组件
	private TextView[] textViews=new TextView[9];
	private ImageView photoView;
	private RelativeLayout houseLayout;
	private Button backBtn;
	
	//数据
	private MyResultList jsonResult;
	private OldhouseAgent detail;
	
	//其他
	private DisplayImageOptions options;
	private Dialog dialog;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_agent_info);
		thisIntent = this.getIntent();
		agentId = thisIntent.getIntExtra("agentId",0);
		agentTel= thisIntent.getStringExtra("agentTel");
		oldhouseList= (OldhouseList) thisIntent.getSerializableExtra("oldhouseList");
		init();
	}
	
	private void init() {

		// Infos
		textViews[0] = (TextView) this.findViewById(R.id.text_Agent_Name);
		textViews[1] = (TextView) this.findViewById(R.id.text_Agent_StarLevel);
		textViews[2] = (TextView) this.findViewById(R.id.text_Agent_Tel);
		textViews[3] = (TextView) this.findViewById(R.id.text_Agent_ServiceArea);
		textViews[4] = (TextView) this.findViewById(R.id.text_Agent_Ranking);
		textViews[5] = (TextView) this.findViewById(R.id.text_Agent_Feedback);
		textViews[6] = (TextView) this.findViewById(R.id.text_Agent_ServiceTime);
		textViews[7] = (TextView) this.findViewById(R.id.text_Agent_HouseCount);
		// 照片
		photoView = (ImageView) this.findViewById(R.id.img_Agent_photo);

		//button
		backBtn=(Button) this.findViewById(R.id.bt_back);
		backBtn.setOnClickListener(this);
		
		//houseLayout
		houseLayout=(RelativeLayout) this.findViewById(R.id.layout_HouseCount);
		houseLayout.setOnClickListener(this);
		
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.old_img_118)
				.showImageForEmptyUri(R.drawable.old_img_118)
				.showImageOnFail(R.drawable.old_img_118).cacheInMemory(true)
				.cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build();
		dialog = new MyProgressDialog(this).createDialog(getResources()
				.getString(R.string.loading));
		// 加载进度条
		dialog.show();
		new Thread(getOldAgentRun).start();
	}
	private void updateUI() {
		// info
		detail=(OldhouseAgent) jsonResult.getData().get(0);
		textViews[0].setText(detail.getName());
		textViews[1].setText(detail.getStarLevel() + "星级");
		textViews[2].setText(detail.getTel());
		textViews[3].setText(detail.getServiceArea());
		textViews[4].setText(detail.getRanking());
		textViews[5].setText(detail.getFeedback());
		textViews[6].setText(detail.getServiceTime());
		textViews[7].setText(detail.getHouseCount());
		// getPhoto
		imageLoader.displayImage(detail.getPhoto(), photoView, options);
	}
	
	private void getDataError(String ERRORMESSAGE) {
		new AlertDialog.Builder(this).setCancelable(false)
				.setMessage(ERRORMESSAGE)
				.setPositiveButton(getResources().getString

				(R.string.cancel), new DialogInterface.OnClickListener()

				{
					public void onClick

					(DialogInterface dialog, int which) {
						finish();
					}
				}).setNegativeButton(getResources().getString

				(R.string.try_again), new DialogInterface.OnClickListener()

				{
					public void onClick(

					DialogInterface dialogInterface, int

					whichButton) {
						dialog.show();
						new Thread(getOldAgentRun).start();
					}
				}).show();
	}
	
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.arg1) {
			case 1:
				updateUI();
				dialog.dismiss();
				break;
			case -1:
				getDataError((String) msg.obj);
				break;
			case 0:
				break;
			}
		}
	};
	
	/**
	 * 线程 得到新房列表
	 */
	Runnable getOldAgentRun = new Runnable() {

		@Override
		public void run() {
			Message msg = handler.obtainMessage();
			String jsonString;
			String url=AppDataConfig.oldUrl
					+ AppDataConfig.OldhouseAgent + "?id=" + agentId;
			Log.wtf("url", url);
			jsonString = HttpTools.getJsonContent(url);
			Log.e("JsonString", jsonString);
			jsonResult = JsonTools.getOldhouseAgent(jsonString);
			if (jsonResult.getSuccess()) {
				msg.arg1 = 1;
			} else {
				msg.arg1 = -1;
				msg.obj = jsonResult.getErrorMessage();
			}
			handler.sendMessage(msg);
		}
	};
	
	public void goAgentHousePage(View v){
		Intent intent=new Intent(this, OldSameCommunityListActivity.class);
		intent.putExtra("agentId", agentId);
		intent.putExtra("agentName", detail.getName());
		intent.putExtra("isAgent", true);
		startActivity(intent);
	}
	
	public void callAgent(View v){
		Intent phoneIntent = new Intent("android.intent.action.CALL",
				Uri.parse("tel:" + agentTel));
		startActivity(phoneIntent);
		callService.saveOldHouse(oldhouseList);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_back:
			this.finish();
			break;
		default:
			break;
		}
		
	}

}